package io.adrainty.bolee.framework.web.converter;

import io.adrainty.bolee.framework.commons.basic.IEnum;
import jakarta.annotation.Nonnull;
import org.springframework.core.convert.converter.Converter;
import org.springframework.core.convert.converter.ConverterFactory;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>EnumConverterFactory</p>
 *
 * @author AdRainty
 * @version V1.0.0
 * @description 枚举转换工厂类
 * @since 2025/8/1 11:17:36
 */

public class EnumConverterFactory implements ConverterFactory<String, IEnum> {

    private static final Map<Class<? extends IEnum>, Converter<String, ? extends IEnum>> CONVERTERS = new HashMap<>();

    @Nonnull
    @Override
    @SuppressWarnings("unchecked")
    public <T extends IEnum> Converter<String, T> getConverter(@Nonnull Class<T> targetType) {
        return  (Converter<String, T>) CONVERTERS.computeIfAbsent(targetType, EnumConverter::new);
    }

}
